package com.itheima.consumer.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Arrays;
import java.util.Map;

@Component
@Slf4j
class SpringRabbitListener {

    /*@RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String message) {
        log.info("王玉洁接收到simple.queue的消息={}", message);
        throw new RuntimeException("我是故意的！"); //返回nack
//        throw new MessageConversionException("我是故意的！"); //返回reject
    }*/

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(Message message) {
        log.info("王玉洁接收到simple.queue的消息id={}", message.getMessageProperties().getMessageId());
        log.info("王玉洁接收到simple.queue的消息体={}", new String(message.getBody()));
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String message) throws InterruptedException {
        System.out.println("消费者1接收到消息=" + message + "," + LocalTime.now());
        Thread.sleep(25);
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String message) throws InterruptedException {
        System.err.println("消费者2...接收到消息=" + message + "," + LocalTime.now());
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanQueue1(String message) {
        log.info("消费者1:监听到fanout.queue1的消息={}", message);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanQueue2(String message) {
        log.info("消费者2:监听到fanout.queue2的消息={}", message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1",durable = "true"),
            exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String message) {
        log.info("消费者1:监听到direct.queue1的消息={}", message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2",durable = "true"),
            exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String message) {
        log.info("消费者2:监听到direct.queue2的消息={}", message);
    }

    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String message) {
        log.info("消费者1:监听到topic.queue1的消息={}", message);
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String message) {
        log.info("消费者2:监听到topic.queue2的消息={}", message);
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String,Object> msg) {
        log.info("object.queue的消息={}", msg);
    }


    //死信交换机
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dlx.queue",durable = "true"),
            exchange = @Exchange(name = "dlx.direct",type = ExchangeTypes.DIRECT),
            key = {"hi"}
    ))
    public void listenDlxQueue(String message) {
        log.info("消费者:监听到 dlx.queue的消息={}", message);
    }

    //延迟插件
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue",durable = "true"),
            exchange = @Exchange(name = "delay.direct",delayed = "true"),
            key = {"hi"}
    ))
    public void listenDelayQueue(String message) {
        log.info("消费者:监听到 delay.queue的消息={}", message);
    }

}